package models.chat;

import org.hibernate.annotations.CollectionOfElements;
import play.db.jpa.Model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Konovalov_Nik
 */
@Entity
public class Member extends Model {
    public String name;

    @ElementCollection
    public List<Long> channelIds = new ArrayList<Long>();
    public Status status;

    public Member(String name) {
        this.name = name;
        this.status = Status.ONLINE;
    }

    public List<Channel> getChannels() {
        List<Channel> channels = new ArrayList<Channel>(channelIds.size());
        for (Long channelId : channelIds) {
            channels.add((Channel) Channel.findById(channelId));
        }
        return channels;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Member)) return false;

        return this.id.equals(((Member) o).id);
    }

    @Override
    public int hashCode() {
        int result = 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (channelIds != null ? channelIds.hashCode() : 0);
        result = 31 * result + (status != null ? status.hashCode() : 0);
        return result;
    }
}
